package com.cn.springcloudwechat.serviceImpl.find;

import com.cn.springcloudwechat.common.mybatis.Page;
import com.cn.springcloudwechat.common.utils.ErrorUtil;
import com.cn.springcloudwechat.mapper.mapper.YmAnswerMapper;
import com.cn.springcloudwechat.mapper.mapper.YmPraiseMapper;
import com.cn.springcloudwechat.mapper.mapper.attachment.YmAttachmentMapper;
import com.cn.springcloudwechat.mapper.mapper.attachment.YmAttachmentRelevanceMapper;
import com.cn.springcloudwechat.mapper.mapper.find.communication.YmConsultMapper;
import com.cn.springcloudwechat.mapper.mapper.find.communication.YmExchangeMapper;
import com.cn.springcloudwechat.model.YmAnswer;
import com.cn.springcloudwechat.model.YmPraise;
import com.cn.springcloudwechat.model.attachment.YmAttachment;
import com.cn.springcloudwechat.model.attachment.YmAttachmentRelevance;
import com.cn.springcloudwechat.model.dto.AttachmentDto;
import com.cn.springcloudwechat.model.find.communication.YmAnswerVO;
import com.cn.springcloudwechat.model.find.communication.YmExchange;
import com.cn.springcloudwechat.model.find.communication.YmExchangeVO;
import com.cn.springcloudwechat.service.find.communication.CommunicationService;
import com.cn.springcloudwechat.upload.CosUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.io.File;
import java.util.*;


@Service("CommunicationService")
public class communicationServiceImpl implements CommunicationService {

    @Autowired
    YmExchangeMapper ymExchangeMapper;


    @Autowired
    YmConsultMapper ymConsultMapper;

    @Autowired
    YmPraiseMapper ymPraiseMapper;

    @Autowired
    YmAnswerMapper ymAnswerMapper;

    @Autowired
    YmAttachmentMapper ymAttachmentMapper;

    @Autowired
    YmAttachmentRelevanceMapper ymAttachmentRelevanceMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertExchange(YmExchange ymExchange) {
        Long userId = ymExchange.getUserId();
        String ecchangeMessage = ymExchange.getEcchangeMessage();
        String title = ymExchange.getTitle();
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        Assert.notNull(ecchangeMessage,String.format(ErrorUtil.PARAMETER_NULL,"ecchangeMessage"));
        Assert.notNull(title,String.format(ErrorUtil.PARAMETER_NULL,"title"));
        ymExchange.setCreateTime(new Date());
        //状态0无效 1有效
        ymExchange.setState(Short.valueOf("1"));
        //类型排序 0官方 1热门 2推荐 3普通
        ymExchange.setTypeOrder(Long.valueOf(3));
        ymExchangeMapper.insertSelective(ymExchange);
        return Long.valueOf(ymExchange.getExchangeId());
    }

    @Override
    public Page<YmExchangeVO>  quertYmExchangeList(Page<YmExchangeVO> page,YmExchangeVO ymExchange) {
        List<YmExchangeVO> ymExchanges = ymExchangeMapper.quertYmExchangeList(page,ymExchange);
        List<AttachmentDto> attachmentDtos = ymAttachmentMapper.selectAllUrl();

        for (YmExchangeVO ymExchangeVO : ymExchanges) {
            List<YmAttachment> ymAttachments = new ArrayList<YmAttachment>();
            for (AttachmentDto attachmentDto: attachmentDtos) {
                YmAttachment ymAttachment = new YmAttachment();
                if (ymExchangeVO.getExchangeId().equals(attachmentDto.getAttId())){
                    ymAttachment.setAttachmentUrl(attachmentDto.getUrl());
                    ymAttachments.add(ymAttachment);
                }
            }
            ymExchangeVO.setYmAttachments(ymAttachments);
        }
        page.setResults(ymExchanges);
        return page;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updatePraiseByUserIdAndTypeId(YmPraise ymPraise) {
        ymPraise.setUpdateTime(new Date());
        return ymPraiseMapper.updatePraise(ymPraise);
    }

    @Override
    public Integer praiseExist(YmPraise ymPraise) {
        //1文章id.2(对医生的提问)评论id).3.交流圈发帖id
        Long praiseTypeId = ymPraise.getPraiseTypeId();
        Long userId = ymPraise.getUserId();
        //点赞类型(1文章2评论3交流圈)
        String praiseType = ymPraise.getPraiseType();
        Assert.notNull(praiseTypeId,String.format(ErrorUtil.PARAMETER_NULL,"praiseTypeId"));
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        Assert.notNull(praiseType,String.format(ErrorUtil.PARAMETER_NULL,"praiseType"));
        return ymPraiseMapper.praiseExist(ymPraise);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer addPraise(YmPraise ymPraise) {
        Long praiseTypeId = ymPraise.getPraiseTypeId();
        Long userId = ymPraise.getUserId();
        Assert.notNull(praiseTypeId,String.format(ErrorUtil.PARAMETER_NULL,"praiseTypeId"));
        Assert.notNull(userId,String.format(ErrorUtil.PARAMETER_NULL,"userId"));
        ymPraise.setState(Short.valueOf("1"));
        ymPraise.setCreateTime(new Date());
        return ymPraiseMapper.insert(ymPraise);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertAnser(YmAnswer ymAnswer,int type) {
        ymAnswer.setType(type);
        //阅读状态 0未读 1已读
        ymAnswer.setReadState(Short.valueOf("0"));
        ymAnswer.setCreateTime(new Date());
        //状态 0无效 1有效
        ymAnswer.setState(Short.valueOf("1"));
        Long typeId = ymAnswer.getTypeId();
        Long answerUserId = ymAnswer.getAnswerUserId();
        String answerDes = ymAnswer.getAnswerDes();
        String url = ymAnswer.getUrl();
        String des = ymAnswer.getAnswerDes();
        Assert.notNull(answerUserId,String.format(ErrorUtil.PARAMETER_NULL,"answerUserId"));
        Assert.notNull(url,String.format(ErrorUtil.PARAMETER_NULL,"url"));
        Assert.notNull(answerDes,String.format(ErrorUtil.PARAMETER_NULL,"answerDes"));
        Assert.notNull(typeId,String.format(ErrorUtil.PARAMETER_NULL,"typeId"));
        Assert.notNull(des,String.format(ErrorUtil.PARAMETER_NULL,"des"));
        return ymAnswerMapper.insertSelective(ymAnswer);
    }

    @Override
    public List<YmAnswerVO> queryYmAnswerList(Long exchangeId,Integer type) {
        List<YmAnswerVO> ymAnswerVOS = ymAnswerMapper.queryYmAnswerList(exchangeId,type);
        for (YmAnswerVO answerVO:ymAnswerVOS
                ) {
            if (null != answerVO.getParentId()){
                String parentName = ymAnswerMapper.queryParentName(answerVO.getParentId());
                answerVO.setParentName(parentName);
            }
        }
        return ymAnswerVOS;
    }

    @Override
    public List<YmAnswerVO> queryYmAnswerDetail(Long typeId,Integer type) {
        List<YmAnswerVO> ymAnswerVOS = ymAnswerMapper.queryYmAnswerDetail(typeId,type);
        for (YmAnswerVO answerVO:ymAnswerVOS
             ) {
            if (null != answerVO.getParentId()){
                String parentName = ymAnswerMapper.queryParentName(answerVO.getParentId());
                answerVO.setParentName(parentName);
            }
        }
        return ymAnswerVOS;
    }

    @Override
    public Integer praiseCount(YmPraise record) {
        String praiseType = record.getPraiseType();
        Long praiseTypeId = record.getPraiseTypeId();
        Assert.notNull(praiseTypeId,String.format(ErrorUtil.PARAMETER_NULL,"praiseTypeId"));
        Assert.notNull(praiseType,String.format(ErrorUtil.PARAMETER_NULL,"praiseType"));

        return ymPraiseMapper.praiseCount(record);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer insertAttachment(String [] ymAttachments,Long relevanceId) {

        for (String ymAttachment : ymAttachments
             ) {
            String[] at = ymAttachment.split("@@@");
            YmAttachment attachment = new YmAttachment();
            //附件名称
            attachment.setAttachmentName(at[0]);
            //附件后缀
            attachment.setAttachmentSuffix(at[1]);
            //将图片附件上传到cos
            String path = at[2];

            attachment.setAttachmentUrl(path);
            //( 1本地服务器 2 oss 3cos 4.文件服务器)
            attachment.setAttachmentType("3");
            int insert = ymAttachmentMapper.insert(attachment);

            YmAttachmentRelevance ymAttachmentRelevance = new YmAttachmentRelevance();
            //业务id
            ymAttachmentRelevance.setRelevanceId(relevanceId);
            //附件id
            ymAttachmentRelevance.setAttachmentId(attachment.getAttachmentId());
            //业务类型 1文章2问医生3交流圈4头像5课程6课程包7图文中的图片
            ymAttachmentRelevance.setRelevanceTpyeId("3");
            ymAttachmentRelevance.setCreateTime(new Date());
            //状态 0无效 1有效
            ymAttachmentRelevance.setState(Short.valueOf("1"));
            int i = ymAttachmentRelevanceMapper.insert(ymAttachmentRelevance);

            if (null == attachment || i == 0){
                return Integer.valueOf("0");
            }
        }
        return Integer.valueOf("1");
    }

    @Override
    public List<YmAttachment> selectByRelevanceIdAndType(YmAttachmentRelevance ymAttachmentRelevance) {
        Long relevanceId = ymAttachmentRelevance.getRelevanceId();
        String relevanceTpyeId = ymAttachmentRelevance.getRelevanceTpyeId();
        Assert.notNull(relevanceId,String.format(ErrorUtil.PARAMETER_NULL,"relevanceId"));
        Assert.notNull(relevanceTpyeId,String.format(ErrorUtil.PARAMETER_NULL,"relevanceTpyeId"));
        return ymAttachmentMapper.selectByRelevanceIdAndType(ymAttachmentRelevance);
    }

    @Override
    public YmExchangeVO selectByPrimaryKey(Long exchangeId) {
        return ymExchangeMapper.selectVoById(exchangeId);
    }

    /**
     * 删除帖子
     *
     * @param exchangeId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteExchangeByExchangeId(String [] exchangeId,String state) {
        Assert.notNull(exchangeId,String.format(ErrorUtil.PARAMETER_NULL,"exchangeId"));
        Integer i=ymExchangeMapper.updateByExchangeId(exchangeId,state);
        return i;
    }

    /**
     * 删除评论
     *
     * @param answerId
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer deleteAnswerByAnswerId(String [] answerId) {
        Assert.notNull(answerId,String.format(ErrorUtil.PARAMETER_NULL,"answerId"));
        Integer i=ymAnswerMapper.updateByAnswerId(answerId);
        return  i;
    }

    @Override
    public Page<YmExchangeVO> quertYmExchangeListMrg(YmExchangeVO ymExchangeVO,Page<YmExchangeVO> page) {
        {
            List<YmExchangeVO> ymExchanges = ymExchangeMapper.quertYmExchangeListMrg(ymExchangeVO, page);
            List<AttachmentDto> attachmentDtos = ymAttachmentMapper.selectAllUrl();

            for (YmExchangeVO ymExchange : ymExchanges) {
                List<YmAttachment> ymAttachments = new ArrayList<YmAttachment>();
                for (AttachmentDto attachmentDto: attachmentDtos) {
                    YmAttachment ymAttachment = new YmAttachment();
                    if (ymExchange.getExchangeId().equals(attachmentDto.getAttId())){
                        ymAttachment.setAttachmentUrl(attachmentDto.getUrl());
                        ymAttachments.add(ymAttachment);
                    }
                }
                ymExchange.setYmAttachments(ymAttachments);
            }
            page.setResults(ymExchanges);
           return page;
        }
    }

    @Override
    public Page<YmAnswerVO> quertCommunicationDetailMrg(YmExchangeVO ymExchangeVO, Page<YmAnswerVO> page) {

        try {
            Long exchangeId = ymExchangeVO.getExchangeId();
            //帖子回复内容
            List<YmAnswerVO> ymAnswerVOS = ymAnswerMapper.queryYmAnswerDetailMrg(exchangeId,3,page);
            for (YmAnswerVO answerVO:ymAnswerVOS
                    ) {
                if (null != answerVO.getParentId()){
                    String parentName = ymAnswerMapper.queryParentName(answerVO.getParentId());
                    answerVO.setParentName(parentName);
                }
            }
            page.setResults(ymAnswerVOS);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }

    @Override
    public Integer todayPraiseCount(YmPraise ymPraise) {
        return ymPraiseMapper.todayPraiseCount(ymPraise);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer updateTypeOrder(String[] exchangeId, String[] typeOrder) {
        Assert.notNull(exchangeId,String.format(ErrorUtil.PARAMETER_NULL,"exchangeId"));
        Assert.notNull(typeOrder,String.format(ErrorUtil.PARAMETER_NULL,"typeOrder"));
        for (int i = 0; i < exchangeId.length; i++) {
            ymExchangeMapper.updateTypeOrder(exchangeId[i], typeOrder[i]);
        }
        return 1;
    }

    @Override
    public YmExchangeVO quertExchangenDetailMrg(YmExchangeVO ymExchangeVO) {
        Assert.notNull(ymExchangeVO.getExchangeId(),String.format(ErrorUtil.PARAMETER_NULL,"exchangeId"));
        //根据帖子id查询
        YmExchangeVO ymExchangeVO1 = ymExchangeMapper.selectVoByIdMrg(ymExchangeVO.getExchangeId());

        //图片附件
        YmAttachmentRelevance ymAttachmentRelevance = new YmAttachmentRelevance();
        ymAttachmentRelevance.setRelevanceId(ymExchangeVO.getExchangeId());
        ymAttachmentRelevance.setRelevanceTpyeId("3");
        List<YmAttachment> ymAttachments = selectByRelevanceIdAndType(ymAttachmentRelevance);
        ymExchangeVO1.setYmAttachments(ymAttachments);

        return ymExchangeVO1;
    }

}
